//
//  HAccountSecurityVC.m
//  BXlive
//
//  Created by huangzhiwen on 2017/4/6.
//  Copyright © 2017年 cat. All rights reserved.
//

#import "HAccountSecurityVC.h"
#import "HHPhoneAccountCell.h"
#import "HAccountCell.h"
#import "HSettingCell.h"
#import <FDFullscreenPopGesture/UINavigationController+FDFullscreenPopGesture.h>
#import <SLDeveloperTools/SLDeveloperTools.h>
#import "BXChagePwdVC.h"
#import "BXPerfectInformationVC.h"
#import <Masonry/Masonry.h>
#import <UMShare/UMShare.h>
#import "SLCancellationAccountVC.h"
#import <SDAutoLayout/SDAutoLayout.h>


@interface HAccountSecurityVC ()<UITableViewDelegate,UITableViewDataSource>

@property(nonatomic,strong)UIView *navigationView;
@property(nonatomic,strong)UITableView *tableView;
@property(nonatomic,strong)NSArray *dataArr;
@property(nonatomic,strong)NSArray *settingArr;
@property(nonatomic,strong)NSArray *imgArr;

@end

@implementation HAccountSecurityVC

- (void)viewWillAppear:(BOOL)animated {
    NSIndexPath *indexPath = [NSIndexPath indexPathForRow:0 inSection:0];
    NSIndexPath *indexPath2 = [NSIndexPath indexPathForRow:0 inSection:1];
    [_tableView reloadRowsAtIndexPaths:@[indexPath, indexPath2] withRowAnimation:UITableViewRowAnimationNone];
}

- (void)viewDidLoad {
    [super viewDidLoad];
    
    self.fd_prefersNavigationBarHidden = YES;
    [self setupNavigationView];
    [self.view setBackgroundColor:[UIColor sl_colorWithHex:0xFFFFFF]];
    
    
//    self.navigationItem.title = NSLocalizedString(@"账号与安全", nil);
//    self.dataArr=@[NSLocalizedString(@"手机号", nil),NSLocalizedString(@"微信账号", nil),NSLocalizedString(@"QQ 账号", nil)];
//    self.imgArr=@[@"icon_phone_login",@"weixin",@"QQ"];
    if ([[BXAppInfo appInfo].ios_app_hidden integerValue] != 1) {
        self.dataArr=@[NSLocalizedString(@"手机号", nil),NSLocalizedString(@"微信账号", nil)];
        self.imgArr=@[@"icon_phone_login",@"weixin"];
    }else{
        self.dataArr=@[NSLocalizedString(@"手机号", nil)];
        self.imgArr=@[@"icon_phone_login"];
    }
    
//    self.settingArr = @[NSLocalizedString(@"修改密码", nil),@"淘宝授权",@"微信号",@"注销账号"];
    self.settingArr = @[NSLocalizedString(@"修改密码", nil),NSLocalizedString(@"注销账号", nil)];
    [self setupTableview];
}

-(void)setupNavigationView{
    _navigationView = [UIView new];
    [self.view addSubview:_navigationView];
    [_navigationView mas_makeConstraints:^(MASConstraintMaker *make) {
        make.left.top.right.mas_equalTo(0);
        make.height.mas_equalTo(__kTopAddHeight+64);
    }];
    [_navigationView setBackgroundColor:[UIColor sl_colorWithHex:0xFFFFFF]];
    WS(weakSelf);
    UIButton *backBtn = [UIButton buttonWithType:UIButtonTypeCustom];
    [backBtn addTarget:self action:@selector(pop) forControlEvents:BtnTouchUpInside];
    [_navigationView addSubview:backBtn];
    [backBtn setImage:CImage(@"back_black") forState:BtnNormal];
    [backBtn mas_makeConstraints:^(MASConstraintMaker *make) {
        make.bottom.left.mas_equalTo(0);
        make.width.height.mas_equalTo(44);
    }];
    UILabel *label = [UILabel createLabelWithFrame:CGRectZero BackgroundColor:SLClearColor Text:NSLocalizedString(@"账号与安全", nil) Font:SLBFont(__ScaleWidth(18)) TextColor:sl_textColors];
    label.textAlignment = 1;
    [_navigationView addSubview:label];
    [label mas_makeConstraints:^(MASConstraintMaker *make) {
        make.left.mas_equalTo(44);
        make.right.mas_equalTo(-44);
        make.centerY.equalTo(weakSelf.navigationView.mas_bottom).offset(-22);
        make.height.mas_equalTo(25);
    }];
}

- (void)setupTableview {
    self.tableView = [[UITableView alloc] initWithFrame:CGRectMake(0, 0, __kWidth, __kHeight - 64 - __kTopAddHeight)];
    [self.view sd_addSubviews:@[self.tableView]];
    WS(weakSelf);
    [self.tableView mas_makeConstraints:^(MASConstraintMaker *make) {
        make.left.bottom.right.mas_equalTo(0);
        make.top.equalTo(weakSelf.navigationView.mas_bottom);
    }];
    self.tableView.dataSource = self;
    self.tableView.delegate = self;
    self.tableView.backgroundColor = [UIColor clearColor];
    self.tableView.separatorStyle = 0;
}

- (void)bindAccountWithType:(NSInteger)type {
    UMSocialPlatformType platformType = UMSocialPlatformType_WechatSession;
    NSString *loginType = @"weixin";
    if (type) {
        platformType = UMSocialPlatformType_QQ;
        loginType = @"qq";
    }
    WS(ws);
    [[UMSocialManager defaultManager] getUserInfoWithPlatform:platformType currentViewController:nil completion:^(id result, NSError *error) {
        if (error) {
            [BGProgressHUD showInfoWithMessage:NSLocalizedString(@"绑定失败", nil)];
        } else {
            [ws requestLogin:result loginType:loginType];
        }
    }];
}

-(void)requestLogin:(UMSocialUserInfoResponse *)result loginType:(NSString *)loginType {
    NSString *gender = @"0";
    if ([result.gender isEqualToString:NSLocalizedString(@"男", nil)]) {
        gender = @"1";
    } else if ([result.gender isEqualToString:NSLocalizedString(@"女", nil)]) {
        gender = @"2";
    }

    [BGProgressHUD showLoadingWithMessage:nil];
    [NewHttpManager bindThirdWithType:loginType openid:result.openid nickname:result.name avatar:result.iconurl gender:gender uuid:result.unionId success:^(NSDictionary *jsonDic, BOOL flag, NSMutableArray *models) {
        [BGProgressHUD hidden];
        if (flag) {
            BXLiveUser *liveUser = [BXLiveUser currentBXLiveUser];
            NSIndexPath *indexPath = nil;
            if (IsEquallString(loginType, @"weixin")) {
                liveUser.bind_weixin = @"1";
                indexPath = [NSIndexPath indexPathForRow:1 inSection:0];
            } else {
                liveUser.bind_qq = @"1";
                indexPath = [NSIndexPath indexPathForRow:2 inSection:0];
            }
            [BXLiveUser setCurrentBXLiveUser:liveUser];
            [self.tableView reloadRowsAtIndexPaths:@[indexPath] withRowAnimation:UITableViewRowAnimationNone];
        } else {
            [BGProgressHUD showInfoWithMessage:jsonDic[@"msg"]];
        }
    } failure:^(NSError *error) {
        [BGProgressHUD showInfoWithMessage:NSLocalizedString(@"绑定失败", nil)];
    }];
}

#pragma - mark UITableView
-(CGFloat)tableView:(UITableView *)tableView heightForRowAtIndexPath:(NSIndexPath *)indexPath {
    return 54;
}

- (NSInteger) numberOfSectionsInTableView:(UITableView *)tableView {
    return  2;
}
- (NSInteger) tableView:(UITableView *)tableView numberOfRowsInSection:(NSInteger)section {
    if (section==0) {
        return self.dataArr.count;
    }
    return self.settingArr.count;
}
- (UITableViewCell *) tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath{
    if (indexPath.section==0) {
        BXLiveUser *liveUser = [BXLiveUser currentBXLiveUser];
        HAccountCell *cell = nil;
        if (!indexPath.row) {
            HHPhoneAccountCell *cell = [HHPhoneAccountCell cellWithTableView:tableView];
            [cell setTitle:self.dataArr[indexPath.row] icon:self.imgArr[indexPath.row] phone:liveUser.phone];
            return cell;
        } else {
            WS(ws);
            cell = [HAccountCell cellWithTableView:tableView];
            cell.type = indexPath.row - 1;
            cell.bindThird = ^(NSInteger type) {
                [ws bindAccountWithType:type];
            };
            BOOL isBind = NO;
            if (indexPath.row == 1) {
                isBind = [liveUser.bind_weixin integerValue];
            } else {
                isBind = [liveUser.bind_qq integerValue];
            }
            [cell setTitle:self.dataArr[indexPath.row] icon:self.imgArr[indexPath.row] isBind:isBind];
            return cell;
        }
    } else {
        static NSString *cellIdentifier = @"SettingCell";
        HSettingCell *cell = [tableView dequeueReusableCellWithIdentifier:cellIdentifier];
        if (cell == nil) {
            cell = [[HSettingCell alloc]initWithStyle:UITableViewCellStyleDefault reuseIdentifier:@"SettingCell"];
        }
        if (indexPath.row == 0) {
            if ([[BXLiveUser currentBXLiveUser].isset_pwd integerValue]) {
                cell.titleLabel.text = NSLocalizedString(@"修改密码", nil);
            } else {
                cell.titleLabel.text = NSLocalizedString(@"设置密码", nil);
            }
            return cell;
        }
        
        cell.titleLabel.text = self.settingArr[indexPath.row];
//        if (indexPath.row == 1) {
//            cell.subTitleLabel.text = @"未授权";
//        }else if (indexPath.row == 2) {
//            cell.subTitleLabel.text = @"填写微信号";
//        }
        return cell;
    }
   
}

-(CGFloat)tableView:(UITableView *)tableView heightForHeaderInSection:(NSInteger)section{
    return section?45+10:45;
}

- (UIView *)tableView:(UITableView *)tableView viewForHeaderInSection:(NSInteger)section
{
    static NSString *ID = @"header";
    UITableViewHeaderFooterView *header = [tableView dequeueReusableHeaderFooterViewWithIdentifier:ID];
    if (header == nil) {
        header = [[UITableViewHeaderFooterView alloc]initWithReuseIdentifier:ID];
        header.contentView.backgroundColor = sl_BGColors;
        UILabel *label = [UILabel initWithFrame:CGRectZero size:14 color:sl_textColors alignment:NSTextAlignmentLeft lines:1];
        label.font = SLBFont(__ScaleWidth(16));
        
        [header.contentView addSubview:label];
        [label mas_makeConstraints:^(MASConstraintMaker *make) {
            make.left.mas_equalTo(__ScaleWidth(12));
            make.right.mas_equalTo(__ScaleWidth(12));
            make.bottom.mas_equalTo(0);
            if (section==1) {
                make.top.mas_equalTo(10);
            }else{
                make.top.mas_equalTo(0);
            }
        }];
        
//        分割视图
        if (section) {
            UIView *fgV = [UIView new];
            [fgV setBackgroundColor: sl_subBGColors];
            [header.contentView addSubview:fgV];
            [fgV mas_makeConstraints:^(MASConstraintMaker *make) {
                make.left.right.top.mas_equalTo(0);
                make.height.mas_equalTo(10);
            }];
        }
//        label.sd_layout.leftSpaceToView(header.contentView, 15).topSpaceToView(header.contentView, 0).bottomSpaceToView(header.contentView, 0).rightSpaceToView(header.contentView, 15);
        
        if (section == 0) {
            label.text = NSLocalizedString(@"账户", nil);
        }else{
            label.text = NSLocalizedString(@"安全", nil);
        }
    }
    return header;
}
- (void)tableView:(UITableView *)tableView didSelectRowAtIndexPath:(NSIndexPath *)indexPath {
    [tableView deselectRowAtIndexPath:indexPath animated:NO];
    
    if (!indexPath.section && !indexPath.row) {
        NSInteger type = 2;
        if (IsNilString([BXLiveUser currentBXLiveUser].phone)) {
            type = 1;
        }
        BXPerfectInformationVC *vc = [[BXPerfectInformationVC alloc] init];
        vc.type = type;
        [self.navigationController pushViewController:vc animated:YES];
    } else if (indexPath.section == 1) {
        if (indexPath.row == 0) {
            BXChagePwdVC *vc = [[BXChagePwdVC alloc]init];
            [self.navigationController pushViewController:vc animated:YES];
//        }else if(indexPath.row == 1){
////            淘宝授权
//        }else if(indexPath.row == 2){
//            微信号
        }else if(indexPath.row == 1){
//            注销账号
            SLCancellationAccountVC *vc = [[SLCancellationAccountVC alloc] init];
            [self pushVc:vc];
        }else if(indexPath.row == 4){
//
        }
        
    }
}

@end
